package cn.yuyiling.jelly.qa.mongodb.repository;

import cn.yuyiling.jelly.qa.mongodb.entity.Question;
import org.springframework.data.mongodb.repository.Aggregation;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;
import java.util.Optional;

public interface QuestionRepository extends MongoRepository<Question, String> {
    Optional<Question> findById(String id);
    long count();
    @Aggregation(pipeline = {
            "{ $lookup: { from: 'answer', localField: '_id', foreignField: 'parentId', as: 'answers' } }",
            "{ $addFields: { answerCount: { $size: '$answers' } } }",
            "{ $addFields: { hotness: { $add: [ { $multiply: ['$likes', 0.4] }, { $multiply: ['$answerCount', 0.6] } ] } } }",
            "{ $sort: { hotness: -1 } }",
            "{ $limit: ?0 }"
    })
    List<Question> findHottestQuestions(int limit);

    @Aggregation(pipeline = {
            "{ $match: { 'knowledge_points': { $in: ?0 } } }", // 根据知识点过滤
            "{ $lookup: { from: 'answer', localField: '_id', foreignField: 'parentId', as: 'answers' } }", // 关联回答
            "{ $addFields: { answerCount: { $size: '$answers' } } }", // 计算回答数
            "{ $addFields: { hotness: { $add: [ { $multiply: ['$likes', 0.4] }, { $multiply: ['$answerCount', 0.6] } ] } } }", // 计算热度
            "{ $sort: { hotness: -1 } }" // 按热度降序排序
    })
    List<Question> findRecommendedQuestions(List<String> knowledgePoints);

    List<Question> findByKnowledgePointsContaining(String knowledgePoint);

    List<Question> findByKnowledgePointsIn(List<String> knowledgePoints);

}